﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;

namespace Speedy
{

    public partial class frmDosDizayn : XtraForm
    {

        public frmDosDizayn()
        {
            InitializeComponent();
        }

        private void xtraTabControl1_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)
        {
            cmb_alan.Properties.Items.Clear();
            cmb_alan.Properties.Items.Add("");
            if (xtraTabControl1.SelectedTabPageIndex == 0)
            {
                for (int i = 0; i <= UST.ItemCount - 1; i++)
                {
                    cmb_alan.Properties.Items.Add(UST.Items[i].ToString().ToUpper());
                }
                txt_satir.Enabled = true;
            }
            else
            {
                for (int i = 0; i <= ALT.ItemCount - 1; i++)
                {
                    cmb_alan.Properties.Items.Add(ALT.Items[i].ToString().ToUpper());
                }
                txt_satir.Text = "";
                txt_satir.Enabled = false;
            }
        }

        private void SimpleButton1_Click(System.Object sender, System.EventArgs e)
        {
            if (xtraTabControl1.SelectedTabPageIndex == 0)
            {
                C1FlexGrid1.Rows.Add();
                if (Bul(0, cmb_alan.Text) == true)
                {
                    C1FlexGrid1[C1FlexGrid1.Rows.Count - 1, 0] = "BTN";
                }
                else
                {
                    C1FlexGrid1[C1FlexGrid1.Rows.Count - 1, 0] = "MS1";
                }
                C1FlexGrid1[C1FlexGrid1.Rows.Count - 1, 1] = cmb_alan.Text;
                C1FlexGrid1[C1FlexGrid1.Rows.Count - 1, 2] = txt_kolon.Text;
                C1FlexGrid1[C1FlexGrid1.Rows.Count - 1, 3] = txt_satir.Text;
                C1FlexGrid1[C1FlexGrid1.Rows.Count - 1, 4] = txt_uzunluk.Text;
            }
            else
            {
                C1FlexGrid2.Rows.Add();
                if (Bul(1, cmb_alan.Text) == true)
                {
                    C1FlexGrid2[C1FlexGrid2.Rows.Count - 1, 0] = "STN";
                }
                else
                {
                    C1FlexGrid2[C1FlexGrid2.Rows.Count - 1, 0] = "MS2";
                }
                C1FlexGrid2[C1FlexGrid2.Rows.Count - 1, 1] = cmb_alan.Text;
                C1FlexGrid2[C1FlexGrid2.Rows.Count - 1, 2] = txt_kolon.Text;
                C1FlexGrid2[C1FlexGrid2.Rows.Count - 1, 3] = txt_hr_start.Text;
                C1FlexGrid2[C1FlexGrid2.Rows.Count - 1, 4] = txt_uzunluk.Text;
            }
            cmb_alan.Text = "";
            txt_kolon.Text = "";
            txt_satir.Text = "";
            txt_uzunluk.Text = "";
        }

        private bool Bul(int tip, string alan)
        {
            bool sonuc = false;
            if (tip == 0)
            {
                for (int i = 0; i <= UST.ItemCount - 1; i++)
                {
                    if (alan == UST.Items[i].ToString().ToUpper())
                    {
                        sonuc = true;
                        break; // TODO: might not be correct. Was : Exit For
                    }
                }
            }
            else
            {
                for (int i = 0; i <= ALT.ItemCount - 1; i++)
                {
                    if (alan == ALT.Items[i].ToString().ToUpper())
                    {
                        sonuc = true;
                        break; // TODO: might not be correct. Was : Exit For
                    }
                }
            }
            return sonuc;
        }

        private void Frm_Dos_Tasarim_Load(object sender, System.EventArgs e)
        {
            //Localizer1.InitLanguage()
            C1FlexGrid1.Cols[0].Visible = false;
            C1FlexGrid2.Cols[0].Visible = false;
            SpinEdit1.Visible = false;
            C1FlexGrid1.Enabled = true;
            C1FlexGrid2.Enabled = true;
            cmb_alan.Properties.Items.Clear();
            cmb_alan.Properties.Items.Add("");
            if (xtraTabControl1.SelectedTabPageIndex == 0)
            {
                for (int i = 0; i <= UST.ItemCount - 1; i++)
                {
                    cmb_alan.Properties.Items.Add(UST.Items[i].ToString().ToUpper());
                }
            }
            else
            {
                for (int i = 0; i <= ALT.ItemCount - 1; i++)
                {
                    cmb_alan.Properties.Items.Add(ALT.Items[i].ToString().ToUpper());
                }
            }
            MatbuuBelgeYazdırToolStripMenuItem.Text = "Matbu Belge Yazdır";
            TextEdit7.EditValue = "FATURA";
        }

        private void txt_hr_start_EditValueChanged(System.Object sender, System.EventArgs e)
        {
            for (int i = 1; i <= C1FlexGrid2.Rows.Count - 1; i++)
            {
                C1FlexGrid2[i, 3] = txt_hr_start.Text;
            }
        }

        private void Dizayn_Kaydet()
        {
            if (Directory.Exists(Application.StartupPath + "\\Tasarim") == true)
            {
                string hed_file = Application.StartupPath + "\\Tasarim\\" + TextEdit7.Text + ".HED";
                string des_file = Application.StartupPath + "\\Tasarim\\" + TextEdit7.Text + ".DES";
                StreamWriter sw = new StreamWriter(hed_file, false);
                sw.WriteLine("Dizayn Tipi         :DOS");
                sw.WriteLine("Font Name           :Arial");
                sw.WriteLine("Font Size           :9,00");
                sw.WriteLine("Dizayn Dosyası      :" + des_file);
                sw.WriteLine("File Path           :" + cmb_printer.Text);
                sw.WriteLine("Toplam Satir Sayisi :" + txt_syf_count.Text);
                sw.WriteLine("Hareket Satir Sayisi:" + txt_hr_count.Text);
                sw.WriteLine("Hareket Baslangıç K.:" + txt_hr_start.Text);
                sw.WriteLine("Alt Toplamlar Sabit :" + chk_alttoplamlar.Checked);
                sw.WriteLine("Alt Toplamlar Kolon :" + txt_alttoplamkolon.Text);
                sw.WriteLine("Satır Birleştir     :" + chkSatirBirlestir.Checked.ToString());
                sw.Close();

                StreamWriter sw2 = new StreamWriter(des_file, false);
                string line = "";
                int kurus = -1;
                bool sonsayfa = false;
                for (int i = 1; i <= C1FlexGrid1.Rows.Count - 1; i++)
                {
                    if (C1FlexGrid1[i, 5] != null) kurus = Convert.ToInt32(C1FlexGrid1[i, 5]); else kurus = -1;
                    sonsayfa = Convert.ToBoolean(C1FlexGrid1[i, 6]);
                    line = C1FlexGrid1[i, 0] + ";" + C1FlexGrid1[i, 1] + ";" + C1FlexGrid1[i, 2] + ";" + C1FlexGrid1[i, 3] + ";" + C1FlexGrid1[i, 4] + ";" + kurus + ";" + sonsayfa;
                    sw2.WriteLine(line);
                }
                line = "";
                kurus = -1;
                for (int i = 1; i <= C1FlexGrid2.Rows.Count - 1; i++)
                {
                    if (C1FlexGrid2[i, 5] != null) kurus = Convert.ToInt32(C1FlexGrid2[i, 5]); else kurus = -1;
                    line = C1FlexGrid2[i, 0] + ";" + C1FlexGrid2[i, 1] + ";" + C1FlexGrid2[i, 2] + ";" + C1FlexGrid2[i, 3] + ";" + C1FlexGrid2[i, 4] + ";" + kurus + ";False";
                    sw2.WriteLine(line);
                }
                sw2.Close();
            }
            else
            {
                MessageBox.Show("Param Klasörü Bulanamadı");
            }
        }
        private void Matbuu_yaz(int tip)
        {
            StreamWriter sw = new StreamWriter(Application.StartupPath.Substring(0, 1) + ":\\matbuu.prn", false);
            int sayi = 0;
            switch (tip)
            {
                case 10:
                    for (int x = 1; x <= 80; x++)
                    {
                        sw.Write(sayi);
                        if (sayi == 9)
                        {
                            sayi = 1;
                        }
                        sayi += 1;
                    }


                    for (int i = 1; i <= int.Parse(txt_syf_count.Text); i++)
                    {
                        if (i > 9)
                        {
                            sw.Write(i + "..............................................................................\r\n");
                        }
                        else if (i > 99)
                        {
                            sw.Write(i + ".............................................................................\r\n");
                        }
                        else
                        {
                            sw.Write(i + "...............................................................................\r\n");
                        }
                    }

                    break;
                case 12:
                    for (int x = 1; x <= 96; x++)
                    {
                        sw.Write(sayi);
                        if (sayi == 9)
                        {
                            sayi = 1;
                        }
                        sayi += 1;
                    }


                    for (int i = 1; i <= int.Parse(txt_syf_count.Text); i++)
                    {
                        if (i > 9)
                        {
                            sw.Write(i + "............................................................................................\r\n");
                        }
                        else if (i > 99)
                        {
                            sw.Write(i + "...........................................................................................\r\n");
                        }
                        else
                        {
                            sw.Write(i + ".............................................................................................\r\n");
                        }
                    }

                    break;
                case 15:
                    for (int x = 1; x <= 120; x++)
                    {
                        sw.Write(sayi);
                        if (sayi == 9)
                        {
                            sayi = 1;
                        }
                        sayi += 1;
                    }



                    for (int i = 1; i <= int.Parse(txt_syf_count.Text); i++)
                    {
                        if (i > 9)
                        {
                            sw.Write(i + ".....................................................................................................................\r\n");
                        }
                        else if (i > 99)
                        {
                            sw.Write(i + "...................................................................................................................\r\n");
                        }
                        else
                        {
                            sw.Write(i + "......................................................................................................................\r\n");
                        }
                    }

                    break;
                case 17:
                    for (int x = 1; x <= 136; x++)
                    {
                        sw.Write(sayi);
                        if (sayi == 9)
                        {
                            sayi = 1;
                        }
                        sayi += 1;
                    }


                    for (int i = 1; i <= int.Parse(txt_syf_count.Text); i++)
                    {
                        if (i > 9)
                        {
                            sw.Write(i + "......................................................................................................................................\r\n");
                        }
                        else if (i > 99)
                        {
                            sw.Write(i + ".....................................................................................................................................\r\n");
                        }
                        else
                        {
                            sw.Write(i + ".......................................................................................................................................\r\n");
                        }
                    }

                    break;
                case 20:
                    for (int x = 1; x <= 160; x++)
                    {
                        sw.Write(sayi);
                        if (sayi == 9)
                        {
                            sayi = 1;
                        }
                        sayi += 1;
                    }


                    for (int i = 1; i <= int.Parse(txt_syf_count.Text); i++)
                    {
                        if (i > 9)
                        {
                            sw.Write(i + "..............................................................................................................................................................\r\n");
                        }
                        else if (i > 99)
                        {
                            sw.Write(i + ".............................................................................................................................................................\r\n");
                        }
                        else
                        {
                            sw.Write(i + "...............................................................................................................................................................\r\n");
                        }
                    }


                    break;
            }



            //sw.Write(Strings.Chr(27) + Strings.Chr(12));
            sw.Close();
            if (File.Exists(Application.StartupPath.Substring(0, 1) + ":\\matbuu.prn"))
            {
                if (cmb_printer.Text.IndexOf("\\\\") > -1)
                {
                    File.Copy(Application.StartupPath.Substring(0, 1) + ":\\matbuu.prn", cmb_printer.Text + "\\matbuu.prn", true);
                }
                else
                {
                    File.Copy(Application.StartupPath.Substring(0, 1) + ":\\matbuu.prn", cmb_printer.Text, true);
                }
                File.Delete(Application.StartupPath.Substring(0, 1) + ":\\matbuu.prn");
            }
        }

        private void Dizayn_Oku()
        {
            string hed_file = "";
            string des_file = "";
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "*.HED|";
            dlg.FileName = "*.HED";
            dlg.FilterIndex = 0;
            if (Directory.Exists(Application.StartupPath + "\\Tasarim") == true) dlg.InitialDirectory = Application.StartupPath + "\\Tasarim";
            dlg.Title = "Dizayn Dosyası";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                hed_file = dlg.FileName;
                string line = "";
                StreamReader sr = new StreamReader(hed_file);
                line = sr.ReadLine();

                if (Strings.Mid(line, 22, Strings.Len(line)) == "DOS")
                {
                    line = sr.ReadLine();
                    line = sr.ReadLine();
                    line = sr.ReadLine();
                    des_file = Strings.Mid(line, 22, Strings.Len(line));

                    line = sr.ReadLine();
                    cmb_printer.Text = Strings.Mid(line, 22, Strings.Len(line));

                    line = sr.ReadLine();
                    txt_syf_count.Text = Strings.Mid(line, 22, Strings.Len(line));

                    line = sr.ReadLine();
                    txt_hr_count.Text = Strings.Mid(line, 22, Strings.Len(line));

                    line = sr.ReadLine();
                    txt_hr_start.Text = Strings.Mid(line, 22, Strings.Len(line));

                    line = sr.ReadLine();
                    chk_alttoplamlar.Checked = bool.Parse(Strings.Mid(line, 22, line.Length));

                    line = sr.ReadLine();
                    txt_alttoplamkolon.Text = Strings.Mid(line, 22, Strings.Len(line));

                    line = sr.ReadLine();
                    if (string.IsNullOrEmpty(line) == false)
                    {
                        chkSatirBirlestir.Checked = bool.Parse(Strings.Mid(line, 22, Strings.Len(line)));
                    }

                    sr.Close();


                    if (File.Exists(des_file) == true)
                    {
                        C1FlexGrid1.Rows.Count = 1;
                        C1FlexGrid2.Rows.Count = 1;
                        StreamReader sr2 = new StreamReader(des_file);
                        string[] ln = null;
                        string tip = "";
                        string alan = "";
                        int kolon = 0;
                        int satir = 0;
                        int uzunluk = 0;
                        int kurus = 0;
                        bool sonsayfa = false;

                        while (sr2.Peek() > -1)
                        {
                            ln = sr2.ReadLine().Split(';');
                            tip = ln[0];
                            alan = ln[1];
                            kolon = int.Parse(ln[2]);
                            satir = int.Parse(ln[3]);
                            uzunluk = int.Parse(ln[4]);
                            sonsayfa = bool.Parse(ln[6]);
                            if (!string.IsNullOrEmpty(ln[5])) kurus = int.Parse(ln[5]); else kurus = -1;

                            switch (Strings.Mid(tip, 1, 3))
                            {
                                case "BTN":
                                    C1FlexGrid1.Rows.Add();
                                    C1FlexGrid1[C1FlexGrid1.Rows.Count - 1, 0] = tip;
                                    C1FlexGrid1[C1FlexGrid1.Rows.Count - 1, 1] = alan;
                                    C1FlexGrid1[C1FlexGrid1.Rows.Count - 1, 2] = kolon;
                                    C1FlexGrid1[C1FlexGrid1.Rows.Count - 1, 3] = satir;
                                    C1FlexGrid1[C1FlexGrid1.Rows.Count - 1, 4] = uzunluk;
                                    if (kurus != -1) C1FlexGrid1[C1FlexGrid1.Rows.Count - 1, 5] = kurus;
                                    C1FlexGrid1[C1FlexGrid1.Rows.Count - 1, 6] = sonsayfa;
                                    break;
                                case "MS1":
                                    C1FlexGrid1.Rows.Add();
                                    C1FlexGrid1[C1FlexGrid1.Rows.Count - 1, 0] = tip;
                                    C1FlexGrid1[C1FlexGrid1.Rows.Count - 1, 1] = alan;
                                    C1FlexGrid1[C1FlexGrid1.Rows.Count - 1, 2] = kolon;
                                    C1FlexGrid1[C1FlexGrid1.Rows.Count - 1, 3] = satir;
                                    C1FlexGrid1[C1FlexGrid1.Rows.Count - 1, 4] = uzunluk;
                                    if (kurus != -1) C1FlexGrid1[C1FlexGrid1.Rows.Count - 1, 5] = kurus;
                                    C1FlexGrid1[C1FlexGrid1.Rows.Count - 1, 6] = sonsayfa;
                                    break;
                                case "STN":
                                    C1FlexGrid2.Rows.Add();
                                    C1FlexGrid2[C1FlexGrid2.Rows.Count - 1, 0] = tip;
                                    C1FlexGrid2[C1FlexGrid2.Rows.Count - 1, 1] = alan;
                                    C1FlexGrid2[C1FlexGrid2.Rows.Count - 1, 2] = kolon;
                                    C1FlexGrid2[C1FlexGrid2.Rows.Count - 1, 3] = satir;
                                    C1FlexGrid2[C1FlexGrid2.Rows.Count - 1, 4] = uzunluk;
                                    if (kurus != -1) C1FlexGrid2[C1FlexGrid2.Rows.Count - 1, 5] = kurus;
                                    break;
                                case "MS2":
                                    C1FlexGrid2.Rows.Add();
                                    C1FlexGrid2[C1FlexGrid2.Rows.Count - 1, 0] = tip;
                                    C1FlexGrid2[C1FlexGrid2.Rows.Count - 1, 1] = alan;
                                    C1FlexGrid2[C1FlexGrid2.Rows.Count - 1, 2] = kolon;
                                    C1FlexGrid2[C1FlexGrid2.Rows.Count - 1, 3] = satir;
                                    C1FlexGrid2[C1FlexGrid2.Rows.Count - 1, 4] = uzunluk;
                                    if (kurus != -1) C1FlexGrid2[C1FlexGrid2.Rows.Count - 1, 5] = kurus;
                                    break;
                            }
                        }
                        sr2.Close();
                    }
                    else
                    {
                        MessageBox.Show("Dizayn Dosyası Bulunamadı");
                    }
                }
                else
                {
                    MessageBox.Show("19150001353", "Dizayn Dosyası Uyumlu Değil");
                }
            }
        }

        private void C1FlexGrid1_Click(System.Object sender, System.EventArgs e)
        {
            Selection_Send();
        }
        private void C1FlexGrid2_Click(System.Object sender, System.EventArgs e)
        {
            Selection_Send();
        }

        private void Selection_Send()
        {
            if (xtraTabControl1.SelectedTabPageIndex == 0)
            {
                if (C1FlexGrid1.Rows.Count > 1)
                {
                    cmb_alan.Text = C1FlexGrid1[C1FlexGrid1.Row, 1].ToString();
                    txt_kolon.Text = C1FlexGrid1[C1FlexGrid1.Row, 2].ToString();
                    txt_satir.Text = C1FlexGrid1[C1FlexGrid1.Row, 3].ToString();
                    txt_uzunluk.Text = C1FlexGrid1[C1FlexGrid1.Row, 4].ToString();
                }
            }
            else
            {
                if (C1FlexGrid2.Rows.Count > 1)
                {
                    cmb_alan.Text = C1FlexGrid2[C1FlexGrid2.Row, 1].ToString();
                    txt_kolon.Text = C1FlexGrid2[C1FlexGrid2.Row, 2].ToString();
                    txt_satir.Text = C1FlexGrid2[C1FlexGrid2.Row, 3].ToString();
                    txt_uzunluk.Text = C1FlexGrid2[C1FlexGrid2.Row, 4].ToString();
                }
            }
        }

        private void SimpleButton4_Click(object sender, EventArgs e)
        {
            Dizayn_Oku();
        }

        private void ana_kaydet_Click(object sender, EventArgs e)
        {
            if (TextEdit7.Text != "")
            {
                Dizayn_Kaydet();
                MessageBox.Show("Dizayn Kaydedildi !");
            }
            else
                MessageBox.Show("Dizayn Adı Belirmemişsiniz !");
        }

        private void SimpleButton2_Click_1(object sender, EventArgs e)
        {
            if (xtraTabControl1.SelectedTabPageIndex == 0)
            {
                if (C1FlexGrid1.Row > 0)
                {
                    C1FlexGrid1.Rows.Remove(C1FlexGrid1.Row);
                }
            }
            else
            {
                if (C1FlexGrid2.Row > 0)
                {
                    C1FlexGrid2.Rows.Remove(C1FlexGrid2.Row);
                }
            }
        }

        private void CpiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Matbuu_yaz(10);
        }

        private void CpiToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Matbuu_yaz(12);
        }

        private void CpiToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            Matbuu_yaz(15);
        }

        private void CpiToolStripMenuItem3_Click(object sender, EventArgs e)
        {
            Matbuu_yaz(17);
        }

        private void CpiToolStripMenuItem4_Click(object sender, EventArgs e)
        {
            Matbuu_yaz(20);
        }

    }

}
